<?php

$php_image_map = [
    '74' => [
        'image' => "php:7.4.33-fpm-bullseye",
    ],
    '80' => [
        'image' => "php:8.0.30-fpm-bullseye",
    ],
    '81' => [
        'image' => "php:8.1.29-fpm-bullseye",
    ],
];

function init_config($php_image_map){
    foreach ($php_image_map as $port => &$info) {

        if (empty($info['name'])) {
            $info['name'] = "php$port";
        }
    
        if (empty($info['port'])) {
            $info['port'] = $port;
        }
    }

    return $php_image_map;
}

$php_image_map = init_config($php_image_map);

function load_file($file, $info)
{
    $content = file_get_contents($file);

    foreach ($info as $key => $value) {
        $content = str_replace('%%'. strtoupper($key) . '%%', $value, $content);
    }

    return $content;
}


$list_files = [
    'config/php.ini',
    'config/zz-www.conf',
    'compose.yaml',
    'DockerFile',
    'build.sh',
    'alias.sh',
];

foreach ($php_image_map as $info) {
    foreach ($list_files as $file) {
        $from_file_path = __DIR__.'/'.'lib/php-dockerfile-tpl/';
        $content = load_file($from_file_path.$file, $info);
        $target_file_path = $info['name'].'/'.$file;
        @mkdir(dirname($target_file_path), 0777, true);
        var_dump($target_file_path);
        file_put_contents($target_file_path, $content);
    }
}
